const fp = require('lodash/fp')
const {Maybe, Container} = require('./support')
//练习1
let maybe = Maybe.of([5, 6, 1]);
let ex1 = () => {
    let fun = (values) => {
        maybe._value = fp.map(v => v + 1, values);
        console.log(maybe);
    };
    maybe.map(fun);
};
//练习2
let xs = Container.of(['do', 'ray',
    'me', 'fa', 'so', 'la', 'ti', 'do']);
let ex2 = () => {
    let fun = (values) => {
        return fp.first(values);
    };

    //其中，xs.map(fun)返回的是Container类对象，输出其中的属性_value
    console.log(xs.map(fun)._value);
};

//练习3
//safeProp函数被简写了
let safeProp = fp.curry((x, o) => Maybe.of(o[x]));
let user = {id: 2, name: 'Albert'};
let ex3 = () => {
    let obj = safeProp('name')(user);
    let fun = (value) => fp.first(value);
    console.log(obj.map(fun)._value);
    //另一种写法
    //let mb = Maybe.of(user);
    //let fun = (sUser) => fp.flowRight(fp.first, fp.prop('name'))(sUser);
    //console.log(mb.map(fun)._value);
};

//练习4
let ex4 = (n) => {
    let mb1 = Maybe.of(n);
    return mb1.map(parseInt)._value;
};

ex1();
ex2();
ex3();
console.log(ex4(14));